/**
 * Created by struggle on 2018/1/4.
 */
angular.module('module.set')
    .controller('SetTimeController', SetTimeController);
SetTimeController.$inject = ['$localStorage', '$stateParams', 'SetService', '$state'];
function SetTimeController($localStorage, $stateParams, SetService, $state) {

    var vm = this;
    vm.save = save;

    if (!$localStorage[environmental + "shopid"]) {
        $localStorage.$reset();
        $state.go('login');
    }

    var shopid = $localStorage[environmental + "shopid"];

    vm.sureBtn = sureBtn; // 模态框
    vm.cancelBtn = cancelBtn; // 模态框

    function sureBtn() {
        vm.messageVisible = false;
    }

    function cancelBtn() {
        vm.messageVisible = false;
    }

    vm.confirmTime = confirmTime;
    vm.deleteTime = deleteTime;
    var timeingid = $stateParams.timeingid;
    vm.timeingid = timeingid;
    vm.open = $stateParams.open;
    vm.close = $stateParams.close;
    active();

    function active() {
        SetService.getTime(shopid, function (data) {
            vm.timeList = data;
            console.log(data);
        });
        if (timeingid) {
            SetService.getTimeByTimeingid(timeingid, function (data) {
                vm.time = data;
            });
        }
        $("#open").shijian({
            showNowTime: false,
            Year: false,//是否显示年//
            Month: false,//是否显示月//
            Day: false,//是否显示日//
            alwaysShow: false,
            successFuc: openFuc
        });
        $("#close").shijian({
            showNowTime: false,
            Year: false,//是否显示年//
            Month: false,//是否显示月//
            Day: false,//是否显示日//
            alwaysShow: false,
            successFuc: closeFuc
        })
    }

    function openFuc(openTime) {
        //保存时间到接口
        vm.open = openTime;
        save();
    }

    function closeFuc(closeTime) {
        //保存时间到接口
        vm.close = closeTime;
        save();
    }

    function save() {
        var myDate = new Date().getTime();
        if (vm.open == null || vm.close == null) {
            return;
        } else if (timeingid) {
            vm.time = {
                "timeingid": timeingid,
                "shopid": shopid,
                "open": vm.open.replace(":", ""),
                "close": vm.close.replace(":", ""),
                "createtime": myDate,
                "type": vm.time.type
            }
        } else {
            vm.time = {
                "timeingid": timeingid,
                "shopid": shopid,
                "open": vm.open.replace(":", ""),
                "close": vm.close.replace(":", ""),
                "createtime": myDate,
                "type": 0
            }
        }

    }

    function confirmTime() {
        if (timeingid) {
            if (vm.time.open >= vm.time.close) {
                alert("关机时间需大于关机时间");
                return;
            }
            if (!judgeNumInArr(vm.time.open, vm.timeList, timeingid)) {
                alert("开机时间已被使用！");
                return;
            }
            if (!judgeNumInArr(vm.time.close, vm.timeList, timeingid)) {
                alert("关机时间已被使用！");
                return;
            }
            if (!judgeNumInArr(vm.time, vm.timeList, timeingid)) {
                alert("该时间段内已设置开关机时间！");
                return;
            }
            SetService.alertTime(vm.time, function (data) {
                history.go(-1);
            })
        } else {
            if (vm.open == null || vm.close == null) {
                vm.messageVisible = true;
                vm.message = "请填写完整！";
                return;
            }
            if (vm.time.open >= vm.time.close) {
                alert("关机时间需大于关机时间");
                return;
            }
            if (!judgeNumInArr(vm.time.open, vm.timeList)) {
                alert("开机时间已被使用！");
                return;
            }
            if (!judgeNumInArr(vm.time.close, vm.timeList)) {
                alert("关机时间已被使用！");
                return;
            }
            if (!judgeNumInArr(vm.time, vm.timeList, timeingid)) {
                alert("该时间段内已设置开关机时间！");
                return;
            }
            SetService.alertTime(vm.time, function (data) {
                history.go(-1);
            })
        }
    }

    function deleteTime() {
        var r = confirm("是否删除营业时间？");
        if (r) {
            SetService.deleteTime(timeingid, function () {
                history.go(-1);
            })

        }
    }

    // 判断时间是否在数组的区间内
    function judgeNumInArr(num, arr, timeingid) {
        var isEffective = true, isObj = false;
        if (typeof num == "object") {
            isObj = true
        }
        for (var i = 0; i < arr.length; i++) {
            var section = arr[i];
            if (isObj) {
                if (section.open >= num.open && section.close <= num.close) {
                    if (timeingid) {
                        if (section.timeingid != timeingid) {
                            isEffective = false;
                        }
                    } else {
                        isEffective = false;
                    }
                }
            } else {
                if (section.open <= num && section.close >= num) {
                    if (timeingid) {
                        if (section.timeingid != timeingid) {
                            isEffective = false;
                        }
                    } else {
                        isEffective = false;
                    }
                }
            }
        }
        return isEffective;
    }
}

